<template>
  <!-- 搜索 -->
  <ContentWrap>
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="房屋地址">
        {{ houseData.name }}
      </el-form-item>
      <el-form-item label="房间地址" prop="keyWord">
        <el-input
          v-model="queryParams.keyWord"
          placeholder="请输入房间地址"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" />搜索</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create', undefined, queryParams.parentId)"
          v-hasPermi="['zwsj:house-room:create']"
        >
          <Icon icon="ep:plus" /> 新增
        </el-button>
        <el-button type="danger" plain @click="synRooms" v-hasPermi="['zwsj:house-room:create']"
          >同步（便民）房间</el-button
        >
        <el-button
          type="danger"
          plain
          @click="showHousElecConfigSett"
          v-hasPermi="['zwsj:house-elec-config:save']"
          >电表参数</el-button
        >
      </el-form-item>
    </el-form>
  </ContentWrap>
  <ContentWrap>
    <el-table v-loading="loading" :data="list">
      <el-table-column label="房间" align="center" prop="shortName" width="80">
        <template #default="scope">
          <span style="color: green" @click="showRoomRents(scope.row.id)">
            {{ scope.row.shortName }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="房租账户" align="center" width="90">
        <template #default="scope">
          <el-button
            type="primary"
            link
            @click="showOwnMeterConfig(scope.row.ownMeterConfigRespVO.id)"
            v-if="scope.row.ownMeterConfigRespVO != null"
          >
              {{scope.row.ownMeterConfigRespVO.configRemark}}
            </el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="租客" align="center" prop="rentCount" width="60" />
      <!-- <el-table-column label="排序" align="center" prop="sort" width="60" /> -->
      <el-table-column label="电表信息" align="center">
        <el-table-column label="" align="center" width="80">
            <div style="color:orange">电表>></div>
        </el-table-column>
        <el-table-column label="使用模式" align="center" prop="meterRespVO.modeFlag" width="90">
          <template #default="scope">
            <span v-if="scope.row.meterRespVO && scope.row.meterRespVO.modeFlag == 0">预付费</span>
            <span
              v-if="scope.row.meterRespVO && scope.row.meterRespVO.modeFlag == 1"
              style="color: red"
              >后付费</span
            >
          </template>
        </el-table-column>
        <!-- <el-table-column label="使用状态" align="center" width="90">
          <template #default="scope">
            <span v-if="scope.row.meterRespVO">
              <dict-tag :type="DICT_TYPE.METER_USE_STATUS" :value="scope.row.meterRespVO.useStatus" />
            </span>
          </template>
        </el-table-column> -->
        <el-table-column label="保电状态" align="center" width="90">
          <template #default="scope">
            <el-tag type="danger" effect="plain" v-if="scope.row.meterRespVO && scope.row.meterRespVO.protectFlag == 1"> 保电中</el-tag>
            <el-tag type="info" effect="plain" v-if="scope.row.meterRespVO && scope.row.meterRespVO.protectFlag == 0"> 不保电 </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="通电状态" align="center" prop="meterRespVO.elecOpenFlag" width="90">
          <template #default="scope">
            <span v-if="scope.row.meterRespVO && scope.row.meterRespVO.elecOpenFlag == 0"
              >通电</span
            >
            <span v-if="scope.row.meterRespVO && scope.row.meterRespVO.elecOpenFlag == 1"
              >断电</span
            >
          </template>
        </el-table-column>
        <el-table-column label="电表编码" align="center" prop="meterRespVO.meterNo" width="130">
          <template #default="scope">
            <el-link
              type="primary"
              v-if="scope.row.meterRespVO"
              @click="showMeter(scope.row.meterRespVO.id)"
              >{{ scope.row.meterRespVO.meterNo }}</el-link
            >
          </template>
        </el-table-column>
        <el-table-column
          label="最后连接时间"
          align="center"
          prop="meterRespVO.lastConnecttime"
          :formatter="dateFormatter"
          width="170"
        />
        <el-table-column label="余额" align="center" prop="meterRespVO.lastBalance" width="100" />
        <el-table-column label="单价" align="center" prop="meterRespVO.price" width="60" />
        <el-table-column label="透支" align="center" prop="meterRespVO.overDraft" width="60" />
      </el-table-column>
      
      <el-table-column label="水表信息" align="center">
        <el-table-column label="" align="center" width="80">
            <div style="color:orange">水表>></div>
        </el-table-column>
        <el-table-column label="水表编码" align="center" width="150">
          <template #default="scope">
            <span v-if="scope.row.waterRespVO">
              <el-link
              type="primary"
              @click="showWater(scope.row.waterRespVO.id)"
              >{{ scope.row.waterRespVO.waterNo }}</el-link
            >
            </span>
          </template>
        </el-table-column>
        <el-table-column label="使用模式" align="center" prop="waterRespVO.modeFlag" width="90">
          <template #default="scope">
            <span v-if="scope.row.waterRespVO && scope.row.waterRespVO.modeFlag == 0">预付费</span>
            <span
              v-if="scope.row.waterRespVO && scope.row.waterRespVO.modeFlag == 1"
              style="color: red"
              >后付费</span
            >
          </template>
        </el-table-column>
        <el-table-column label="余额" align="center" width="90">
          <template #default="scope">
            <span v-if="scope.row.waterRespVO">{{ scope.row.waterRespVO.balance }}元</span>
          </template>
        </el-table-column>
        <el-table-column label="抄收时间" align="center" width="170">
          <template #default="scope">
            <span v-if="scope.row.waterRespVO">{{ formatDate(scope.row.waterRespVO.lastReadTime, 'YYYY-MM-DD HH:mm:ss') }}</span>
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column label="操作" align="center" width="120">
        <template #default="scope">
          <el-button
            type="primary"
            link
            @click="openForm('update', scope.row.id, scope.row.parentId)"
            v-hasPermi="['zwsj:house-room:update']"
          >
            修改
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['zwsj:house-room:delete']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 添加或修改房间对话框 -->
  <RoomForm ref="formRef" @success="getList" />
  <MeterView ref="showMeterRef" />
  <HouseElecConfigSett ref="showHouseElecConfigSettRef" />
  <RoomRentsQuery ref="roomRentsQueryRef" />
  <OwnMeterConfigView ref="showOwnMeterConfigRef" />
  <WaterView ref="showWaterRef" />
</template>
<script lang="ts" setup>
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import { formatDate } from '@/utils/formatTime'
import * as HouseRoomApi from '@/api/zwsj/houseRoom'
import { ElNotification } from 'element-plus'
import RoomForm from './RoomForm.vue'
import HouseElecConfigSett from './HouseElecConfigSett.vue'
import MeterView from '@/views/zwsj/meter/MeterView.vue'
import RoomRentsQuery from './RoomRentsQuery.vue'
import OwnMeterConfigView from '@/views/zwsj/ownMeterConfig/OwnMeterConfigView.vue'
import WaterView from '@/views/zwsj/water/WaterView.vue'
const route = useRoute() // 路由

defineOptions({ name: 'Room' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  parentId: route.params.houseId,
  commId: undefined,
  keyWord: undefined
})
const queryFormRef = ref() // 搜索的表单

/** 查询房产  */
const houseData = ref({
  addrCode: undefined,
  name: undefined
})
const getHouse = async () => {
  try {
    const data = await HouseRoomApi.getHouseRoom(queryParams.parentId)
    houseData.value = data
    queryParams.commId = houseData.value.commId
    getList()
  } finally {
  }
}

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await HouseRoomApi.getHouseRoomPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number, parentId?: number) => {
  formRef.value.open(type, id, parentId)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await HouseRoomApi.deleteHouseRoom(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

const synRooms = async () => {
  //需要判断地址编码是否为空
  let addrCode = houseData.value.addrCode || ''
  if ('' === addrCode) {
    ElNotification({
      title: `失败}`,
      message: h('i', { style: 'color: teal' }, `地址编码不能为空，抽取房间数据失败`)
    })
    return
  }
  ElMessageBox.confirm('此操作将会把从福建公安便民中抽取房产对应的房间信息, 是否继续?', 'warning', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(() => {
      HouseRoomApi.copyRoomsFromBm(addrCode).then((data) => {
        if (data) {
          ElNotification({
            type: 'success',
            title: `成功`,
            message: '数据抽取成功'
          })
          handleQuery()
        }
      })
    })
    .catch(() => {})
}

const showMeterRef = ref()
const showMeter = async (meterId: number) => {
  showMeterRef.value.open(meterId)
}

//批量设置电表参数
const showHouseElecConfigSettRef = ref()
const showHousElecConfigSett = async () => {
  showHouseElecConfigSettRef.value.open(queryParams.parentId)
}

const roomRentsQueryRef = ref()
const showRoomRents = async (roomId: number) => {
  roomRentsQueryRef.value.open(roomId)
}

const showOwnMeterConfigRef = ref()
const showOwnMeterConfig = async (ownMeterConfigId: number) => {
  showOwnMeterConfigRef.value.open(ownMeterConfigId)
}


/** 显示水表 */
const showWaterRef = ref()
const showWater = async (waterId: number) => {
  showWaterRef.value.open(waterId)
}

/** 初始化 */
onMounted(() => {
  getHouse()
})
</script>
